"use strict";

/**
 * 日期格式转换
 * @param fmt
 * @returns {*}
 */
Date.prototype.format = function (fmt) {
    var weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
    var o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "w+": weekday[this.getDay()],                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
};

/**
 * 色值 RGB转换为16进制  "rgb(255,255,255)".colorHex();  // #ffffff
 * @returns {string}
 */
String.prototype.colorHex = function () {
    // RGB颜色值的正则
    var reg = /^(rgb|RGB)/;
    var color = this;
    if (reg.test(color)) {
        var strHex = "#";
        // 把RGB的3个数值变成数组
        var colorArr = color.replace(/(?:\(|\)|rgb|RGB)*/g, "").split(",");
        // 转成16进制
        for (var i = 0; i < colorArr.length; i++) {
            var hex = Number(colorArr[i]).toString(16);
            if (hex === "0") {
                hex += hex;
            }
            strHex += hex;
        }
        return strHex;
    } else {
        return String(color);
    }
};

/**
 * 色值 16进制转换为RGB  "#fff".colorRgb();// 255,255,255  "#ffffff".colorRgb();// 255,255,255
 * @returns {string}
 */
String.prototype.colorRgb = function () {
    // 16进制颜色值的正则
    var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
    // 把颜色值变成小写
    var color = this.toLowerCase();
    if (reg.test(color)) {
        // 如果只有三位的值，需变成六位，如：#fff => #ffffff
        if (color.length === 4) {
            var colorNew = "#";
            for (var i = 1; i < 4; i += 1) {
                colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
            }
            color = colorNew;
        }
        // 处理六位的颜色值，转为RGB
        var colorChange = [];
        for (var i = 1; i < 7; i += 2) {
            colorChange.push(parseInt("0x" + color.slice(i, i + 2)));
        }
        return colorChange.join(",");
    } else {
        return color;
    }
};

/**
 * 绘制图表
 * @param domId
 * @param option
 * @param click
 */
function eChart_draw(domId, option, click) {
    var dom = document.getElementById(domId);
    var myChart = echarts.init(dom);
    $(dom).resize(function () { //容器大小变化时，表重新自适应 ，$("selector").resize()依赖jquery和jqResize.js
        myChart.resize();
    });
    myChart.setOption(option, true);

    if (click) { //点击事件
        myChart.on('click', function (param) {
            click(param);
        });
    }
}

/**
 * 封装jquery的ajax，公用错误提示信息，加载loading
 * @param options
 */
function iAjax(options) {
    // console.log(options);
    var layerLoading;
    $.ajax({
        url: options.url,
        type: options.type,
        data: options.data,
        contentType: options.contentType,
        dataType: options.dataType,
        timeout: options.timeout || 30000,
        headers: {
            UserId: _AuthUserId
        },
        success: function (ret) {
            // console.log(ret);
            options.success(ret);
        },
        error: function (err) {
            // console.log(err);
            if (options.error) {
                options.error(err);
            } else {
                layer.alert("链接丢失！请尝试刷新页面或重新登录。", {icon: 5, closeBtn: 0, btnAlign: 'c'}, function (index) {
                    layer.close(index);
                });
            }
        },
        beforeSend: function (xhr) {
            if (!options.noLoading) {
                layerLoading = layer.load(0, {shade: 0.1});
            }
            if (options.beforeSend) {
                options.beforeSend(xhr);
            }
        },
        complete: function (xhr) {
            if (!options.noLoading) {
                layer.close(layerLoading);
            }
            if (options.complete) {
                options.complete(xhr);
            }
        }
    });
}

/**
 * 获取当前页面地址的参数值
 * @param name 参数名
 * @returns {null}
 */
function getLocHrefParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = decodeURI(window.location.search.substr(1)).match(reg);//对中文解码
    if (r != null) return unescape(r[2]);
    return null;
}

/**
 * 危险操作前的确认
 * @param str @string 操作名称
 * @param func @function 操作
 */
function dangerAction(str, func) {
    var msg = "是否" + str + "？";
    layer.confirm(msg, {title: [str, 'background:#2D3F51;color:#FFFFFF;'], shadeClose: true}, function (index) {
        func();
        layer.close(index);
    });
}